//
//  XCMNearbyTableViewController.m
//  糗事百科
//
//  Created by 奚超明 on 16/6/12.
//  Copyright © 2016年 奚超明. All rights reserved.
//

#import "XCMNearbyTableViewController.h"
#import "NearbyModel.h"
#import "NearbyCell.h"

@interface XCMNearbyTableViewController ()
/** 模型数组 */
@property (nonatomic, strong) NSMutableArray *modelArr;
/** 记录当前页码 */
@property (nonatomic, assign) NSUInteger page;

@end

@implementation XCMNearbyTableViewController

static NSString * const nearbyID = @"nearbyID";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 标题
    self.navigationItem.title = @"附近的人";
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NearbyCell class]) bundle:nil] forCellReuseIdentifier:nearbyID];
    
    // 取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 行高
    self.tableView.rowHeight = 90;
    
    // 添加刷新控件
    [self addRefresh];
}

#pragma mark - 刷新控件
- (void)addRefresh
{
    // 页码为1
    self.page = 1;
    
    // 添加下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    // 添加上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    // 默认一进来就下拉刷新
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 下拉刷新添加新数据
- (void)loadNewData
{
    // 去除所有数据
    [self.modelArr removeAllObjects];
    
    [self.tableView.mj_footer endRefreshing];
    
    self.page = 1;
    
    NSString *file = [[NSBundle mainBundle] pathForResource:@"nearby1" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:file];
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *nearbyArr = rootDic[@"nearby"];
    for (NSDictionary *nearbyDic in nearbyArr) {
        NearbyModel *model = [NearbyModel nearbyWithDic:nearbyDic];
        [self.modelArr addObject:model];
    }
    
    // 刷新表视图
    [self.tableView reloadData];
    
    // 结束下拉刷新
    [self.tableView.mj_header endRefreshing];
}

#pragma mark - 上拉刷新添加更多数据
- (void)loadMoreData
{
    // 页码加1
    self.page++;
    
    NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"nearby%zd", self.page] ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:file];
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *nearbyArr = rootDic[@"nearby"];
    for (NSDictionary *nearbyDic in nearbyArr) {
        NearbyModel *model = [NearbyModel nearbyWithDic:nearbyDic];
        [self.modelArr addObject:model];
    }
    
    // 刷新表视图
    [self.tableView reloadData];
    
    // 判断有没有更多数据
    if (self.page >= 9) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
}

#pragma mark - 判断上拉刷新控件的显示与隐藏
- (void)checkFooterhidden
{
    if (self.modelArr.count != 0) {
        self.tableView.mj_footer.hidden = NO;
    } else {
        self.tableView.mj_footer.hidden = YES;
    }
}

#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [self checkFooterhidden];
    return self.modelArr.count;
}

#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NearbyCell *cell = [tableView dequeueReusableCellWithIdentifier:nearbyID];
    cell.nearbyModel = self.modelArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - 懒加载
- (NSMutableArray *)modelArr
{
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}

@end
